Priority Queue Algorithm

The Priority Queue Algorithm is a data structure that is designed to manage a collection of elements with different priorities, wherein elements with higher priorities are processed before those with lower priorities. It is an abstract data type that supports two main operations: insertion of elements with an associated priority, and removal of the element with the highest priority. Priority queues can be implemented using various data structures, such as arrays, linked lists, heaps, or binary trees, with each having its own advantages and trade-offs in terms of time and space complexity. Heaps, especially binary heaps, are commonly used as they provide a good balance between efficiency and simplicity. Priority Queue Algorithm finds its applications in various real-world scenarios and computer science problems. Some common use cases include scheduling processes in operating systems, managing events in discrete event simulations, and implementing algorithms like Dijkstra's shortest path and A* search, which rely on efficiently selecting the highest priority element to explore next. The efficient management of priority queues is crucial in these scenarios, as it directly impacts the performance of the systems or algorithms that depend on it. Overall, the Priority Queue Algorithm is an essential tool for handling data with varying priorities, enabling efficient solutions for a wide range of problems.
/*
 Petar 'PetarV' Velickovic
 Data Structure: Priority Queue (Binary Heap)
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 1000001
using namespace std;
typedef long long lld;

int PQ[MAX_N];
int heap_size = 0;

//Prioritetni red implementiran kao min-heap
//Slozenost: O(log N) za Push/Pop, O(1) za Empty/GetMinimum

inline bool Empty()
{
    return (heap_size == 0);
}

inline int GetMinimum()
{
    if (Empty()) return -1;
    return PQ[1];
}

inline void Push(int x)
{
    PQ[++heap_size] = x;
    int pos = heap_size;
    while (pos > 1 && PQ[pos/2] > PQ[pos])
    {
        swap(PQ[pos/2], PQ[pos]);
        pos /= 2;
    }
}

inline void Pop()
{
    if (Empty()) return;
    int pos = 1;
    swap(PQ[pos], PQ[heap_size--]);
    while (pos <= heap_size)
    {
        int ret = pos;
        int left = pos*2;
        int right = pos*2+1;
        if (left <= heap_size && PQ[left] < PQ[ret]) ret = left;
        if (right <= heap_size && PQ[right] < PQ[ret]) ret = right;
        if (ret != pos)
        {
            swap(PQ[pos], PQ[ret]);
            pos = ret;
        }
        else break;
    }
}

int main()
{
    Push(5);
    Push(3);
    Push(8);
    Push(4);
    Push(11);
    while (!Empty())
    {
        printf("%d ",GetMinimum());
        Pop();
    }
    printf("\n");
    return 0;
}

LANGUAGE:

DARK MODE: